//
//  SDWindowManager.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDWindowManager.h"
#import "SDNavigationController.h"
#import "SDPhotoBrowserViewController.h"
#import "SDSettingsViewController.h"
#import "SDTabBarController.h"
#import "SDLoginViewController.h"
#import "SDPaientListViewController.h"
#import "SDLoginManager.h"
#import "SDPhotoInfoViewController.h"
#import "AAPLCameraViewController.h"
#import "SDGuideLoginViewController.h"
#import "SDAppConfigManager.h"
#define Level_UserWindow 5

@interface SDWindowManager()
{
    UIWindow*  _userWindow;
    SDTabBarController* _tabBarCtrler;
    SDNavigationController* _userNavViewController;
}
@end

@implementation SDWindowManager
+ (SDWindowManager*)defaultManager
{
    static SDWindowManager *windowManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        windowManager = [[SDWindowManager alloc] init];
        
    });
    return  windowManager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeGuideViewController) name:kSipLoginNotificaion object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginDidFailNotification:) name:kLoginServiceeDidFailNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginDidSuccessNotification:) name:kLoginMgrDidSucceedNotificaion object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerDidSuccessNotification:) name:kRegisterDidSucceedNotification object:nil];

    }
    return self;
}

-(void)initUserInterface
{
   // [[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    [self setupMainViewController];
}

-(void)setupMainViewController
{
    _tabBarCtrler = [[SDTabBarController alloc] init];
    [[SDAppConfigManager sharedInstance] setBootTabIndex:1];

    NSMutableArray* tmpArr = [NSMutableArray arrayWithObjects:[[SDPhotoBrowserViewController alloc] init],
                              [[AAPLCameraViewController alloc] init],
                              [[SDPaientListViewController alloc] init],[[SDSettingsViewController alloc] init],nil];
    
    _tabBarCtrler.viewControllers = tmpArr;
    _rootNavigationCtl  = [[SDNavigationController alloc]initWithRootViewController:_tabBarCtrler];
    self.appKeyWindow.rootViewController  = _rootNavigationCtl;
    //[self showGuideViewController];
    SDLoginManager* loginMgr = [SDLoginManager sharedInstance];
    if (![loginMgr isLogin]) {
        [self showLoginView];
    } else
    {
        [self enterMainView];
    }
}

-(void)showLoginView
{
    [self showUserWindow:[[SDGuideLoginViewController alloc] init]];
}

-(void)showUserWindow:(SDBaseViewController*)viewController
{
    if (!_userWindow)
    {
        _userWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _userWindow.backgroundColor = [UIColor whiteColor];
        _userNavViewController = [[SDNavigationController alloc] initWithRootViewController:viewController];
        _userWindow.rootViewController = _userNavViewController;
        [_userWindow makeKeyAndVisible];
    }
}

-(void)enterMainView
{
    [_userNavViewController.view removeFromSuperview];
    [_userWindow removeFromSuperview];
    _userNavViewController = nil;
    [self.appKeyWindow makeKeyAndVisible];
    _userWindow = nil;
    
 //   [[NSNotificationCenter defaultCenter] postNotificationName:Notifition_RemoveGuideViewController object:nil];
}

- (void)removeGuideViewController
{
//    if (VP_ACCOUNTINFO_SINGLETON(isNewUser) && VP_LOGIN_SINGLETON(isLogin))
//        //   if(1)
//    {
//        [self showUserTasteViewController];
//    }
//    else
//    {
//        [self enterMainView];
//    }
    
    [self enterMainView];
    
}
- (UIWindow*)topWindow
{
    if (_userWindow)
    {
        return _userWindow;
    }
    else
    {
        return self.appKeyWindow;
    }
}

-(SDTabBarController*)getTabBarController
{
    return _tabBarCtrler;
}

- (UIViewController*)currentShowController
{
    if (_userWindow)
    {
        return _userNavViewController.topViewController;
    }
    
    if (_rootNavigationCtl.viewControllers.count >1)
    {
        return _rootNavigationCtl.topViewController;
    }
    else
    {
        return _tabBarCtrler.selectedViewController;
    }
    
}

#pragma mark -
#pragma mark -  Login Notifications
- (void)loginDidSuccessNotification:(NSNotification *)not
{
    SDLoginManager* loginMgr = [SDLoginManager sharedInstance];
    if ([loginMgr isLogin] && [_userWindow isKeyWindow]) {
        [self enterMainView];
    }
}

- (void)loginDidFailNotification:(NSNotification *)not
{
    
}

- (void)registerDidSuccessNotification:(NSNotification *)not
{
    SDLoginManager* loginMgr = [SDLoginManager sharedInstance];
    if ([loginMgr isLogin] && [_userWindow isKeyWindow]) {
        [self enterMainView];
    }
}
@end
